package com.supermap.database.geo;

import com.alibaba.fastjson.JSONArray;
import org.geotools.data.DataStore;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.AttributeDescriptor;


import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/9/10 10:24
 */
public class GetShpDataStore {

    private File receiveFile;
    private SimpleFeatureSource simpleFeatureSource;
    private SimpleFeatureCollection simpleFeatureCollection;

    public GetShpDataStore(File file){
        this.receiveFile = file;
        getDataStore();
    }

    public DataStore getDataStore() {
        FileDataStore fileDataStore = null;
        try {
            fileDataStore = FileDataStoreFinder.getDataStore(this.receiveFile);
            // 下面的设置是让中文传输进去好读出来  到底是哪一个还不明确
            //      最新研究表明，通过桌面导出的数据是 GBK 的，外面的一般是 UTF-8 的
//            ((ShapefileDataStore) fileDataStore).setCharset(Charset.forName("utf-8"));
//            ((ShapefileDataStore) fileDataStore).setCharset(Charset.forName("GBK"));
            simpleFeatureSource = fileDataStore.getFeatureSource();
            simpleFeatureCollection = simpleFeatureSource.getFeatures();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileDataStore;
    }

    /** 获取文件属性 ID 字段*/
    public JSONArray getAttributeID(){
        JSONArray jsonArrayId = new JSONArray();
        List<AttributeDescriptor> attrList= this.simpleFeatureSource.getSchema().getAttributeDescriptors();
        for(AttributeDescriptor attr : attrList){
            String ID = attr.getName().getLocalPart();
            if( !ID.equals("the_geom")){
                jsonArrayId.add(ID);
            }
        }
        return jsonArrayId;
    }

    public SimpleFeatureCollection getSimpleFeatureCollection(){
        return simpleFeatureCollection;
    }

    /** 获取文件 name 字段*/
    public String getFileName(){
        String fileName = null;
        try {
            fileName = getDataStore().getTypeNames()[0];
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /** 获取文件 feature 的个数*/
    public int getFeatureCounts(){
        int featureCount = simpleFeatureCollection.size();
        return featureCount;
    }

    /** 获取文件 feature 的类型*/
    public String getFeatureType(){
        SimpleFeatureIterator featureIterator = simpleFeatureCollection.features();
        String type = "";
        while (featureIterator.hasNext()) {
            SimpleFeature feature = featureIterator.next();
            // 获得 geometry
            Object geomObj = feature.getDefaultGeometry();
            Geometry geometry = (Geometry)geomObj;
            type = geometry.getGeometryType();
            break;
        }
        featureIterator.close();
        return type;
    }
}
